package com.eksboot;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.DefaultThreadFactory;

import java.util.concurrent.ThreadFactory;

/**
 * Socket
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-04-26 15:13
 * @blame Team
 */
public class NettyClient {

    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    public static final String SS = "aa55016f000134343131343430310000000000000000000000000000000000000000000000000038333462623730303862643161616163383837666661363961376538363838350035363133374441303442413535413633363736343837413331303630454335450000000000bade";

    public static byte[] toBytes(String str) {
        if(str == null || str.trim().length() == 0) {
            return new byte[0];
        }
        byte[] bytes = new byte[str.length() / 2];
        for(int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }
        return bytes;
    }

    public static String bytesToHex(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int index = 0;
        // 利用位运算进行转换，可以看作方法一的变种
        for(byte b : bytes) {
            buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
            buf[index++] = HEX_CHAR[b & 0xf];
        }
        return new String(buf);
    }

    public static void main(String[] args) throws InterruptedException {

        System.out.println(new String(toBytes(SS)));

//        EventLoopGroup workerGroup = new NioEventLoopGroup();
//        try {
//            Bootstrap b = new Bootstrap()
//                    .group(workerGroup)
//                    .channel(NioSocketChannel.class)
//                    .option(ChannelOption.SO_BACKLOG, 1024)
//                    .option(ChannelOption.SO_KEEPALIVE, true)
//                    .option(ChannelOption.TCP_NODELAY, true)
//                    .handler(new LoggingHandler(LogLevel.INFO))
//                    .handler(new ChannelInitializer<SocketChannel>() {
//                        @Override
//                        public void initChannel(SocketChannel ch) throws Exception {
//                            ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4));
//                            ch.pipeline().addLast(new LengthFieldPrepender(4));
//                            //字符串解码
//                            ch.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
//                            //字符串编码
//                            ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
//
//                            ch.pipeline().addLast(new TerminalChannelHandler());
//                        }
//                    });
//            ChannelFuture f = b.connect("192.168.102.228", 8500).sync();
//            Channel channel = f.channel();
//            channel.writeAndFlush(toBytes(SS));
//            channel.closeFuture().sync();
//            System.out.println("....");
//        } catch(Exception e) {
//            e.printStackTrace();
//        }
    }
}
